
//
//  EruptyViewController.m
//  肿瘤
//
//  Created by 无梦生 on 16/9/14.
//  Copyright © 2016年 Sampson. All rights reserved.
//

#import "EruptyViewController.h"
#import "DisModel.h"
#import <SVProgressHUD.h>

@interface EruptyViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,weak)UITableView * tableView;

@property (nonatomic,strong)NSArray * eruptyArray;

@property (nonatomic,assign)NSInteger index;

@property (nonatomic,strong)NSMutableArray *tagArray;

@property (nonatomic,strong)NSMutableArray * saveArray;

@end

@implementation EruptyViewController

-(NSMutableArray *)saveArray
{
    if (!_saveArray) {
        _saveArray = [NSMutableArray array];
    }
    return _saveArray;
}

-(NSMutableArray *)tagArray
{
    if (!_tagArray) {
        _tagArray = [NSMutableArray array];
    }
    return _tagArray;
}

-(void)setEruptyArray:(NSArray *)eruptyArray
{
    _eruptyArray = eruptyArray;
    
    [self.tableView reloadData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    DisModel * model = [[DisModel alloc]init];
    
    NSString * urlstring = @"http://iosapi.itcast.cn/complicationList.json.php";
    
    NSDictionary *paramDic = @{@"page":@1,@"page_size":@15,@"ci2_id":@3
                               };
    [model disWithUrlstring:urlstring papmr:paramDic Success:^(NSArray<DisModel *> *array) {
        
        self.eruptyArray = [array valueForKey:@"complication_name"];
        
    } Error:^{
        
      
        
    }];
}

-(UIButton *)subsbutton
{
    NSInteger btnX = self.view.bounds.size.width - 60;
    
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(btnX, 4, 55, 32)];
    
    btn.layer.cornerRadius = 5;
    
    btn.clipsToBounds = YES;
    
    [btn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [btn setTitle:@"订阅" forState:UIControlStateNormal];
    
    [btn  setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    
    [btn setTitle:@"已订阅" forState:UIControlStateSelected];
    
    [btn addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}

-(void)changeType:(UIButton *)btn
{
    
    if (btn.selected == NO) {
        
        [self.tagArray addObject:@(btn.tag)];
        
        [btn setSelected:YES];
        
        self.index =  btn.tag;
    }else
    {
        [self.tagArray removeObject:@(btn.tag)];
        
        [btn setSelected:NO];
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"并发症";
    
    UIBarButtonItem * bbt = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(popView:)];
    
    self.navigationItem.rightBarButtonItem = bbt;
    
    self.tableView.backgroundColor = [UIColor lightGrayColor];
    
    [self creatTableView];
}

-(void)creatTableView
{
    UITableView * tableView  = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    tableView.dataSource = self;
    
    tableView.delegate = self;
    
    self.tableView = tableView;
    
    [self.view addSubview:tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.eruptyArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    
    cell.textLabel.text = self.eruptyArray[indexPath.row];
    
    UIButton * btn = [self subsbutton];
    
    btn.tag = indexPath.row;
    
    cell.accessoryView = btn;
    
    return cell;
}
-(void)popView:(UIButton*)btn
{
    
    if (self.block)
    {
        //遍历已经获得Btn的Tag值
        [self.tagArray enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSString * string = [NSString stringWithFormat:@"%@",self.eruptyArray[obj.intValue]];
            //            //将数组的文字添加到可变数组中
            [self.saveArray addObject:string];
            //            //将可变数组的文字变成NSString输出
            NSString * str =  [self.saveArray componentsJoinedByString:@"丶"];
            
            self.block(str);
        }];
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
